import actionTypes from './actionTypes'

// 2.获取actiontype
const handleSeeting = () => {
    return {
        type: actionTypes.SETTING_THEME
    }
}

// 设置成功之后的请求
const seetingSuccess = (data) => {
    return {
        type: actionTypes.SEETING_THEME_SUCCESS,
        payload: {
            data
        }
    }
}

export const seetingHandle = (data) => {
    return dispatch => {
        // 派送actiontype
        dispatch(handleSeeting())
        getSomeData()
            .then(resp => {
                if (resp.data.code === 200) {
                    const {
                        authToken,
                        ...userInfo
                    } = resp.data.data
                    if (userInfo.remember === true) {
                        window.localStorage.setItem('authToken', authToken)
                        window.localStorage.setItem('userInfo', JSON.stringify(userInfo))
                    } else {
                        window.sessionStorage.setItem('authToken', authToken)
                        window.sessionStorage.setItem('userInfo', JSON.stringify(userInfo))
                    }
                    dispatch(loginSuccess(resp.data.data))
                } else {
                    dispatch(loginFailed())
                }
            })
    }
}

// 模拟一个promise
const getSomeData = () => {
    return new Promise((res, rej) => {
        setTimeout(() => {
            res({
                id: '1',
                name: 'zhangshan',
                sex: 'nan',
                age: 12
            })
        }, 2000)
    })
}